package com.jsoft.controller;

import com.alibaba.fastjson.JSON;
import com.jsoft.entity.wang.EnterpriseName;
import com.jsoft.entity.wang.Transfer;
import com.jsoft.service.wang.EnterpriceNameService;
import com.jsoft.service.wang.TransferService;
import com.jsoft.service.wang.impl.EnterpriceNameServiceImpl;
import com.jsoft.service.wang.impl.TransferServiceImpl;
import com.jsoft.util.IsNumeric;
import com.jsoft.util.Message;
import com.jsoft.util.PageHelper;
import com.jsoft.util.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 王兆吉
 * @email 593020160@qq.com
 * @date 2023/2/22
 */
@WebServlet("*.trans")
@MultipartConfig
public class TransferController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        String servletPath = req.getServletPath();
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1, servletPath.length() - 6);
        try {
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }

    private void query(HttpServletRequest req, HttpServletResponse resp) {
        TransferService transferService = new TransferServiceImpl();
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        PageHelper<Transfer> pageHelper = new PageHelper<>();
        String currentPage = req.getParameter("currentPage");
        String enterpriseId = req.getParameter("enterpriseId");
        currentPage = currentPage == null || ("".equals(currentPage)) ? "1" : currentPage.trim();
        pageHelper.setCurrentPage(Integer.parseInt(currentPage));
        String sql1 = "select count(*) from transfer where 1=1   and  transfer_enterprise_id= " + enterpriseId;
        String sql = "SELECT distinct transfer.id,no ,\n" +
                "    amount ,\n" +
                "    other.name as openEnterpriseName,\n" +
                "    enterprise.name as receiveEnterpriseName ,\n" +
                "    instituty.name as institutyName ,\n" +
                "    open_time as openTime ,\n" +
                "    transfer_time as transferTime ,\n" +
                "    expiry_time as expiryTime ,\n" +
                "    uplink_address as uplinkAddress ,\n" +
                "    status ,\n" +
                "    progress_status as progressStatus  FROM transfer,enterprise,other,instituty WHERE enterprise.id = open_enterprise_id " +
                "    and other.id = receive_enterprise_id " +
                "    and instituty.id = instituty_id " +
                "    and  transfer_enterprise_id= " + enterpriseId;
        BufferedReader reader = null;
        try {
            reader = req.getReader();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String json = null;
        try {
            json = reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
//        try {
        //把json串转成对象↓
        Transfer transfer = JSON.parseObject(json, Transfer.class);

        if (transfer.getNo() != null) {
            sql += " AND no like '%" + transfer.getNo() + "%'";
            sql1 += " AND no like '%" + transfer.getNo() + "%'";
        }
        if (transfer.getOpenTime() != null) {
            Date openTime = transfer.getOpenTime();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            String strDate1 = sdf1.format(openTime);

            sql += " AND open_time  BETWEEN '" + strDate1 + " 00:00:00 '" + " and '" + strDate1 + " 23:59:59 '";
            sql1 += " AND open_time  BETWEEN '" + strDate1 + " 00:00:00 '" + " and '" + strDate1 + " 23:59:59 ' ";
        }
        if (transfer.getReceiveEnterpriseId() != null) {
            sql += " AND receive_enterprise_id = " + transfer.getReceiveEnterpriseId();
            sql1 += " AND receive_enterprise_id = " + transfer.getReceiveEnterpriseId();
        }
        if (transfer.getOpenEnterpriseId() != null) {
            sql += " AND open_enterprise_id = " + transfer.getOpenEnterpriseId();
            sql1 += " AND open_enterprise_id = " + transfer.getOpenEnterpriseId();
        }

            if (transfer.getAmount1() != null) {
                sql += " AND amount >= " + transfer.getAmount1() * 10000;
                sql1 += " AND amount >= " + transfer.getAmount1() * 10000;
            }
            if (transfer.getAmount2() != null) {
                sql += " AND amount <= " + transfer.getAmount2() * 10000;
                sql1 += " AND amount <= " + transfer.getAmount2() * 10000;
            }
            if (transfer.getStatus() != null) {
                sql += " AND status like '%" + transfer.getStatus() + "%'";
                sql1 += " AND status like '%" + transfer.getStatus() + "%'";
            }
            sql += " order by transfer_time desc ";
            sql += " limit ? , ? ";
            try {
                pageHelper.setTotal(transferService.getRecordsTotal(sql1));
                Integer start = pageHelper.getStart();
                Integer each = pageHelper.getPageSize();
                List<Transfer> trans = transferService.showAllTickets(sql, start, each);
                pageHelper.setData(trans);
                out.write(JSON.toJSONString(new ResultUtil("trans", pageHelper)));
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                out.close();
            }
    }

    private void dropDown1(HttpServletRequest req, HttpServletResponse resp) {
        EnterpriceNameService enterpriceNameService = new EnterpriceNameServiceImpl();
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            List<EnterpriseName> allEnterpriseName = enterpriceNameService.showAllEnterprise();
            out.write(JSON.toJSONString(new ResultUtil("allEnterpriseName", allEnterpriseName)));
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

}
